import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

import name.raynal.tool.SebUrlFile;
import drive.DriveLocations;
import exception.URLNotFoundException;

public class RenameMain {

	private static final String TEMP_LOCATION = DriveLocations.CLYDE
			+ "/Documents/RDVX";

	public static void main(String[] args) {
		titles = new HashMap<String, String>();
		System.out.println("START");
		new File(TEMP_LOCATION).mkdirs();

		SebUrlFile mainPage = null;
		try {
			mainPage = new SebUrlFile(TEMP_LOCATION + "/index.htm");
			mainPage.copyURLcontent("http://rendezvousavecmrx.free.fr/page/liste_pc.php?tri=3");
		} catch (URLNotFoundException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String content = mainPage.getContent();

		int count = 0;
		while (content.contains("mr_x")) {
			int index = content.indexOf("mr_x");
			String name = content.substring(index, index + 19);
			content = content.substring(index + "mr_x".length());
			index = content.indexOf("cle_emission");
			content = content.substring(index + "cle_emission".length());
			int indexOpen = content.indexOf(">");
			int indexClose = content.indexOf("<");
			String title = content.substring(indexOpen + 1, indexClose);
			System.out.println(name + " " + title);
			rename(name, title);
			count++;
		}
		System.out.println("Count = " + count);

		try {
			Runtime.getRuntime().exec("explorer \"" + TEMP_LOCATION + "\"");
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("DONE");
	}

	private static void rename(String name, String title) {
		File from = new File(TEMP_LOCATION, name);
		File to = new File(TEMP_LOCATION, title + ".mp3");
		if (from.exists()) {
			from.renameTo(to);
		}
	}
}
